/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.jms;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import net.huangp.domain.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;

/**
 *
 * @author Patrick Huang
 */
@Component
public class JmsProducer {
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------
    private static final Logger logger = LoggerFactory.getLogger(JmsProducer.class);
    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    private JmsTemplate jmsTemplate;
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    @Autowired
    public JmsProducer(JmsTemplate jmsTemplate){
        this.jmsTemplate = jmsTemplate;
    }

    public void sendMessage(final long tranId,final String message){
        jmsTemplate.send(new MessageCreator() {

            public Message createMessage(Session session) throws JMSException {
                TextMessage jmsMessage = session.createTextMessage(message);
                jmsMessage.setLongProperty(Transaction.TRANSACTION_ID, tranId);
                return jmsMessage;
            }
        });
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
}
